/**
  ******************************************************************************
  * File Name          : ctrl_mode.c
  * Description        : Code for data process applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tskcfg.h"
#include "ctrl_common.h"
#include "spiflash.h"
#include "bsp.h"
#include <stdlib.h>

volatile Ctrl_StateFNType Ctrl_StateFN, Ctrl_StateFN_last;
volatile Ctrl_StateSLType Ctrl_StateSL, Ctrl_StateSL_last;
volatile Ctrl_StateNQType Ctrl_StateNQ, Ctrl_StateNQ_last;
volatile Ctrl_StateVFType Ctrl_StateVF, Ctrl_StateVF_last;
volatile Ctrl_StateDKType Ctrl_StateDK, Ctrl_StateDK_last;

Ctrl_ParaMDType  Ctrl_ParaMD;
Ctrl_ParaJTType  Ctrl_ParaJT;
Ctrl_ParaDL1Type Ctrl_ParaDL1;
Ctrl_ParaDL2Type Ctrl_ParaDL2;
Ctrl_ParaSKType  Ctrl_ParaSK;
Ctrl_ParaTDType  Ctrl_ParaTD;
Ctrl_ParaDKType  Ctrl_ParaDK;

static Ctrl_StorageMDType ctrl_storage_md;
static Ctrl_StorageJTType ctrl_storage_jt;
static Ctrl_StorageDL1Type ctrl_storage_dl1;
static Ctrl_StorageDL2Type ctrl_storage_dl2;
static Ctrl_StorageSKType ctrl_storage_sk;
static Ctrl_StorageTDType ctrl_storage_td;
static Ctrl_StorageDKType ctrl_storage_dk;

const static Ctrl_StorageMDType ctrl_storage_md_init = {
  .key = CTR_STORAGE_MD_KEY,
  
  .para.fnmdsken = 0,
  .para.slmdsken = 0,
  .para.nqmdsken = 0,
  .para.vfmdsken = 0,
  .para.mode     = CTRL_MODE_MU
};

const static Ctrl_StorageJTType ctrl_storage_jt_init = {
  .key = CTR_STORAGE_JT_KEY,
  
  .para.fnsptp = 5,
  .para.fnrttp = 2,
  .para.fnhin3 = 50,
  .para.slrttp = 2,
  .para.nqfq   = 0,
  .para.nqrttp = 2,
  .para.fnhitp = 250,
  .para.fnlotp = 150,
  .para.slhitp = 300,
  .para.sllotp = 200,
  .para.nqhitp = 200,
  .para.nqlotp = 300,
  .para.bjgw   = 280,
  .para.bjdw   = 120,
  .para.xcfnins = 10,
};

const static Ctrl_StorageDL1Type ctrl_storage_dl1_init = {
  .key = CTR_STORAGE_DL1_KEY,
  
  .para.fntp[0]  = 250,
  .para.fntp[1]  = 250,
  .para.fntp[2]  = 250,
  .para.fntp[3]  = 250,
  .para.fntp[4]  = 250,
  .para.fntp[5]  = 250,
  .para.fntp[6]  = 250,
  .para.fntp[7]  = 250,
  .para.fntp[8]  = 250,
  .para.fntp[9]  = 250,
  .para.fntp[10] = 250,
  .para.fntp[11] = 250,
  .para.fntp[12] = 250,
  
  .para.sltp[0]  = 300,
  .para.sltp[1]  = 300,
  
  .para.nqtp[0]  = 200,
  .para.nqtp[1]  = 200,
  .para.nqtp[2]  = 200,
  
  .para.vftp[0]  = 250,
  .para.vftp[1]  = 250,
  .para.vftp[2]  = 250,
  .para.vftp[3]  = 250,
};

const static Ctrl_StorageDL2Type ctrl_storage_dl2_init = {
  .key = CTR_STORAGE_DL2_KEY,
  
  .para.fnrttp   = 2,
  .para.slrttp   = 2,
  .para.nqfq     = 0,
  .para.nqrttp   = 2,
};

const static Ctrl_StorageSKType ctrl_storage_sk_init = {
  .key = CTR_STORAGE_SK_KEY,
  
  .para.fnen     = 0,
  .para.fntd[0]  = 0,
  .para.fntd[1]  = 0,
  .para.fntd[2]  = 0,
  .para.fntd[3]  = 0,
  .para.fntd[4]  = 0,
  .para.fntd[5]  = 0,
  .para.fntd[6]  = 0,
  .para.fntd[7]  = 0,
  .para.fntd[8]  = 0,
  .para.fntd[9]  = 0,
  .para.fntd[10] = 0,
  .para.fntd[11] = 0,
  .para.fntd[12] = 0,
  
  .para.slen     = 0,
  .para.sltd[0]  = 0,
  .para.sltd[1]  = 0,
  
  .para.nqen     = 0,
  .para.nqtd[0]  = 0,
  .para.nqtd[1]  = 0,
  .para.nqtd[2]  = 0,
  
  .para.vfen     = 0,
  .para.vftf[0]  = 1,
  .para.vftf[1]  = 1,
  .para.vftf[2]  = 1,
  .para.vftf[3]  = 1,
  .para.vfjs[0]  = 0,
  .para.vfjs[1]  = 0,
  .para.vfjs[2]  = 0,
  .para.vfjs[3]  = 0,
  
  .para.fnkq     = 180,
  .para.fngb     = 300,

  .para.slkq     = 180,
  .para.slgb     = 300,

  .para.nqkq     = 180,
  .para.nqgb     = 300,

  .para.vfkq[0]  = 180,
  .para.vfgb[0]  = 300,
  .para.vfkq[1]  = 180,
  .para.vfgb[1]  = 300,
  .para.vfkq[2]  = 180,
  .para.vfgb[2]  = 300,
  .para.vfkq[3]  = 180,
  .para.vfgb[3]  = 300,
};

const static Ctrl_StorageTDType ctrl_storage_td_init = {
  .key = CTR_STORAGE_TD_KEY,
  
  .para.fntd[0]  = 1,
  .para.fntd[1]  = 1,
  .para.fntd[2]  = 1,
  .para.fntd[3]  = 1,
  .para.fntd[4]  = 0,
  .para.fntd[5]  = 0,
  .para.fntd[6]  = 0,
  .para.fntd[7]  = 0,
  .para.fntd[8]  = 0,
  .para.fntd[9]  = 0,
  .para.fntd[10] = 0,
  .para.fntd[11] = 0,
  .para.fntd[12] = 0,
  
  .para.sltd[0]  = 1,
  .para.sltd[1]  = 0,
  
  .para.nqtd[0]  = 1,
  .para.nqtd[1]  = 0,
  .para.nqtd[2]  = 0,
  
  .para.vftd[0]  = 1,
  .para.vftd[1]  = 0,
  .para.vftd[2]  = 0,
  .para.vftd[3]  = 0,
  
  .para.bjtd     = 0,
  .para.zden     = 0,
  .para.zdtd     = 0,
};

const static Ctrl_StorageDKType ctrl_storage_dk_init = {
  .key = CTR_STORAGE_DK_KEY,
  
  .para.dktm[0].dkkqtm1 = 0,
  .para.dktm[0].dkgbtm1 = 0,
  .para.dktm[0].dkkqtm2 = 0,
  .para.dktm[0].dkgbtm2 = 0,
  
  .para.dktm[1].dkkqtm1 = 0,
  .para.dktm[1].dkgbtm1 = 0,
  .para.dktm[1].dkkqtm2 = 0,
  .para.dktm[1].dkgbtm2 = 0,
  
  .para.dktm[2].dkkqtm1 = 0,
  .para.dktm[2].dkgbtm1 = 0,
  .para.dktm[2].dkkqtm2 = 0,
  .para.dktm[2].dkgbtm2 = 0,
  
  .para.dktm[3].dkkqtm1 = 0,
  .para.dktm[3].dkgbtm1 = 0,
  .para.dktm[3].dkkqtm2 = 0,
  .para.dktm[3].dkgbtm2 = 0,
  
  .para.dktm[4].dkkqtm1 = 0,
  .para.dktm[4].dkgbtm1 = 0,
  .para.dktm[4].dkkqtm2 = 0,
  .para.dktm[4].dkgbtm2 = 0,
  
  .para.dktm[5].dkkqtm1 = 0,
  .para.dktm[5].dkgbtm1 = 0,
  .para.dktm[5].dkkqtm2 = 0,
  .para.dktm[5].dkgbtm2 = 0,
  
  
  .para.tgjs[0] = 100,
  .para.tgjs[1] = 100,
  .para.tgjs[2] = 100,
  .para.tgjs[3] = 100,
};

void Control_CreateTask(void)
{
	Ctr_InitState();
	
  xControl_Thd   =  xTaskCreateStatic(vControl_Task,
																	   "CRTL",
																	   CONTROL_STACK_SIZE,
																	   (void *)1,
																	   CONTROL_TSK_PRIORITY,
																	   xControl_Stk,
																	   &xControl_Tcb);
	
	xCtrVfd_Thd   =  xTaskCreateStatic(vCtrVfd_Task,
																	   "vfd",
																	   CTRVFD_STACK_SIZE,
																	   (void *)1,
																	   CTRVFD_TSK_PRIORITY,
																	   xCtrVfd_Stk,
																	   &xCtrVfd_Tcb);
}

/*****************************************************************************/

/*****************************************************************************/
void Ctr_InitState(void)
{
	memset((void *)&Ctrl_StateFN_last, '0', sizeof(Ctrl_StateFNType));
	memset((void *)&Ctrl_StateSL_last, '0', sizeof(Ctrl_StateSLType));
	memset((void *)&Ctrl_StateNQ_last, '0', sizeof(Ctrl_StateNQType));
	memset((void *)&Ctrl_StateVF_last, '0', sizeof(Ctrl_StateVFType));
  memset((void *)&Ctrl_StateDK_last, 1,   sizeof(Ctrl_StateDKType));
	
	memset((void *)&Ctrl_StateFN, 'n', sizeof(Ctrl_StateFNType));
	memset((void *)&Ctrl_StateSL, 'n', sizeof(Ctrl_StateSLType));
	memset((void *)&Ctrl_StateNQ, 'n', sizeof(Ctrl_StateNQType));
	memset((void *)&Ctrl_StateVF, 'n', sizeof(Ctrl_StateVFType));
	memset((void *)&Ctrl_StateDK, 0,   sizeof(Ctrl_StateDKType));
  
	Ctrl_StateVF.vfjs[0] = 0;
	Ctrl_StateVF.vfjs[1] = 0;
	Ctrl_StateVF.vfjs[2] = 0;
	Ctrl_StateVF.vfjs[3] = 0;
}

uint8_t Ctr_CheckStateCorrect(const char c)
{
	if(c == 'J')
	{
		return 0;
	}
	else if(c == 'j')
	{
		return 0;
	}
	else if(c == 'R')
	{
		return 0;
	}
	else if(c == 'r')
	{
		return 0;
	}
	else if(c == 'l')
	{
		return 0;
	}
	else if(c == 'S')
	{
		return 0;
	}
	else if(c == 's')
	{
		return 0;
	}
	else if(c == 'D')
	{
		return 0;
	}
	else if(c == 'd')
	{
		return 0;
	}
	else if(c == 'E')
	{
		return 0;
	}
	else if(c == 'e')
	{
		return 0;
	}
	else if(c == 'T')
	{
		return 0;
	}
	else if(c == 't')
	{
		return 0;
	}
	else if(c == 'p')
	{
		return 0;
	}
	else if(c == 'q')
	{
		return 0;
	}
	else if(c == 'Z')
	{
		return 0;
	}
	else if(c == 'z')
	{
		return 0;
	}
	else if(c == 'X')
	{
		return 0;
	}
	else if(c == 'x')
	{
		return 0;
	}
	else if(c == 'Y')
	{
		return 0;
	}
	else if(c == 'y')
	{
		return 0;
	}
	else if(c == 'w')
	{
		return 0;
	}
	else if(c == 'M')
	{
		return 0;
	}
	else if(c == 'm')
	{
		return 0;
	}
	else if(c == 'n')
	{
		return 0;
	}
  else if(c == 'A')
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

uint8_t Ctr_CheckMDPara(Ctrl_ParaMDType *para)
{
  if( (para->mode != CTRL_MODE_JT) &&
      (para->mode != CTRL_MODE_DL) &&
      (para->mode != CTRL_MODE_SK) &&
      (para->mode != CTRL_MODE_ZN) &&
      (para->mode != CTRL_MODE_MU) )
  {
    return 1;
  }
  
  if((para->fnmdsken != 0) && (para->fnmdsken != 1))
  {
    return 2;
  }
  
  if((para->slmdsken != 0) && (para->slmdsken != 1))
  {
    return 3;
  }
  
  if((para->nqmdsken != 0) && (para->nqmdsken != 1))
  {
    return 4;
  }
  
  if((para->vfmdsken != 0) && (para->vfmdsken != 1))
  {
    return 5;
  }
  
	return 0;
}

uint8_t Ctr_CheckJTPara(Ctrl_ParaJTType *para)
{
  if((para->fnhitp <0) || ( para->fnhitp >990))
  {
    return 1;
  }
  
  if((para->fnlotp <0) || ( para->fnlotp >990))
  {
    return 2;
  }
  
  if(para->fnhin3 > 254)
  {
    return 3;
  }
  
  if(para->fnsptp > 50)
  {
    return 4;
  }
  
  if(para->fnrttp > 20)
  {
    return 5;
  }
  
  if((para->slhitp <0) || ( para->slhitp >990))
  {
    return 6;
  }
  
  if((para->sllotp <0) || ( para->sllotp >990))
  {
    return 7;
  }
  
  if(para->slrttp > 20)
  {
    return 8;
  }
  
  if((para->nqfq != 0) && (para->nqfq != 1))
  {
    return 9;
  }
  
  if((para->nqhitp <0) || ( para->nqhitp >990))
  {
    return 10;
  }
  
  if((para->nqlotp <0) || ( para->nqlotp >990))
  {
    return 11;
  }
  
  if(para->nqrttp > 20)
  {
    return 12;
  }
  
  if((para->bjgw <0) || ( para->bjgw >990))
  {
    return 13;
  }
  
  if((para->bjdw <0) || ( para->bjdw >990))
  {
    return 14;
  }
  
  if(para->xcfnins > 100)
  {
    return 15;
  }
  
	return 0;
}

uint8_t Ctr_CheckDL1Para(Ctrl_ParaDL1Type *para)
{
  uint8_t i;
  
  for(i=0; i<13; i++)
  {
    if((para->fntp[i] <0) || (para->fntp[i] > 990))
    {
      return 1;
    }
  }
  
  for(i=0; i<2; i++)
  {
    if((para->sltp[i] <0) || (para->sltp[i] > 990))
    {
      return 2;
    }
  }
  
  for(i=0; i<3; i++)
  {
    if((para->nqtp[i] <0) || (para->nqtp[i] > 990))
    {
      return 3;
    }
  }
  
  for(i=0; i<4; i++)
  {
    if((para->vftp[i] <0) || (para->vftp[i] > 990))
    {
      return 4;
    }
  }
  
	return 0;
}

uint8_t Ctr_CheckDL2Para(Ctrl_ParaDL2Type *para)
{
  if(para->fnrttp > 20)
  {
    return 1;
  }
  
  if(para->slrttp > 20)
  {
    return 2;
  }
  
  if(para->nqrttp > 20)
  {
    return 3;
  }
  
  if((para->nqfq != 0) && (para->nqfq != 1))
  {
    return 4;
  }
  
	return 0;
}

uint8_t Ctr_CheckSKPara(Ctrl_ParaSKType *para)
{
  uint8_t i;
  
  if((para->fnen != 0) && (para->fnen != 1))
  {
    return 1;
  }
  
  for(i=0; i<13; i++)
  {
    if((para->fntd[i] != 0) && (para->fntd[i] != 1))
    {
      return 2;
    }
  }
  
  if((para->slen != 0) && (para->slen != 1))
  {
    return 3;
  }
  
  for(i=0; i<2; i++)
  {
    if((para->sltd[i] != 0) && (para->sltd[i] != 1))
    {
      return 4;
    }
  }
  
  if((para->nqen != 0) && (para->nqen != 1))
  {
    return 5;
  }
  
  for(i=0; i<3; i++)
  {
    if((para->nqtd[i] != 0) && (para->nqtd[i] != 1))
    {
      return 6;
    }
  }
  
  if((para->vfen != 0) && (para->vfen != 1))
  {
    return 7;
  }
  
  if(para->fnkq > 3600)
  {
    return 8;
  }
  
  if(para->fngb > 3600)
  {
    return 9;
  }
  
  if(para->slkq > 3600)
  {
    return 10;
  }
  
  if(para->slgb > 3600)
  {
    return 11;
  }
  
  if(para->nqkq > 3600)
  {
    return 12;
  }
  
  if(para->nqgb > 3600)
  {
    return 13;
  }
  
  for(i=0; i<4; i++)
  {
    if(para->vfkq[i] > 3600)
    {
      return 14;
    }
  }
  
  for(i=0; i<4; i++)
  {
    if(para->vfgb[i] > 3600)
    {
      return 15;
    }
  }
  
  for(i=0; i<4; i++)
  {
    if((para->vftf[i] != 0) && (para->vftf[i] != 1))
    {
      return 16;
    }
  }
  
  for(i=0; i<4; i++)
  {
    if(para->vfjs[i] > 10)
    {
      return 17;
    }
  }
  
	return 0;
}

uint8_t Ctr_CheckTDPara(Ctrl_ParaTDType *para)
{
  uint8_t i;
  
  for(i=0; i<13; i++)
  {
    if((para->fntd[i] != 0) && (para->fntd[i] != 1))
    {
      return 1;
    }
  }
  
  for(i=0; i<2; i++)
  {
    if((para->sltd[i] != 0) && (para->sltd[i] != 1))
    {
      return 1;
    }
  }
  
  for(i=0; i<3; i++)
  {
    if((para->nqtd[i] != 0) && (para->nqtd[i] != 1))
    {
      return 3;
    }
  }
  
  for(i=0; i<4; i++)
  {
    if((para->vftd[i] != 0) && (para->vftd[i] != 1))
    {
      return 4;
    }
  }
  
  if((para->bjtd != 0) && (para->bjtd != 1))
  {
    return 5;
  }
    
  if((para->zdtd != 0) && (para->zdtd != 1))
  {
    return 6;
  }
  
  if(para->zden != 0)
  {
    return 7;
  }
  
	return 0;
}

uint8_t Ctr_CheckDKPara(Ctrl_ParaDKType *para)
{
  uint8_t i;
  
  for(i=0; i<DKCTR_PT_CHANNEL_ALLNUM; i++)
  {
    if(para->dktm[i].dkgbtm1 > 86400u)
    {
      return 1;
    }
    
    if(para->dktm[i].dkgbtm1 > 86400u)
    {
      return 2;
    }
    
    if(para->dktm[i].dkkqtm2 > 86400u)
    {
      return 3;
    }
    
    if(para->dktm[i].dkgbtm2 > 86400u)
    {
      return 4;
    }
  }
  
  for(i=0; i<DKCTR_TG_CHANNEL_ALLNUM; i++)
  {
    if(para->tgjs[i] > 100)
    {
      return 5;
    }
  }
  
  return 0;
}

static void ctr_save_init_md(void)
{
	sFlash_SectorErase(SFLASH_CTRMD_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_CTRMD_ADDR,  (uint8_t *)&ctrl_storage_md_init,  sizeof(Ctrl_StorageMDType));
	
	Ctrl_ParaMD = ctrl_storage_md_init.para;
	
	SysUsrLog("MD InitPara saved success!\r\n");
}

static void ctr_save_init_jt(void)
{
	sFlash_SectorErase(SFLASH_CTRJT_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_CTRJT_ADDR,  (uint8_t *)&ctrl_storage_jt_init,  sizeof(Ctrl_StorageJTType));
	
	Ctrl_ParaJT = ctrl_storage_jt_init.para;
	
	SysUsrLog("JT InitPara saved success!\r\n");
}

static void ctr_save_init_dl1(void)
{
	sFlash_SectorErase(SFLASH_CTRDL1_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_CTRDL1_ADDR,  (uint8_t *)&ctrl_storage_dl1_init,  sizeof(Ctrl_StorageDL1Type));
	
	Ctrl_ParaDL1 = ctrl_storage_dl1_init.para;
	
	SysUsrLog("DL1 InitPara saved success!\r\n");
}

static void ctr_save_init_dl2(void)
{
	sFlash_SectorErase(SFLASH_CTRDL2_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_CTRDL2_ADDR,  (uint8_t *)&ctrl_storage_dl2_init,  sizeof(Ctrl_StorageDL2Type));
	
	Ctrl_ParaDL2 = ctrl_storage_dl2_init.para;
	
	SysUsrLog("DL2 InitPara saved success!\r\n");
}

static void ctr_save_init_sk(void)
{
	sFlash_SectorErase(SFLASH_CTRSK_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_CTRSK_ADDR,  (uint8_t *)&ctrl_storage_sk_init,  sizeof(Ctrl_StorageSKType));
	
	Ctrl_ParaSK = ctrl_storage_sk_init.para;
	
	SysUsrLog("SK InitPara saved success!\r\n");
}

static void ctr_save_init_td(void)
{
	sFlash_SectorErase(SFLASH_CTRTD_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_CTRTD_ADDR,  (uint8_t *)&ctrl_storage_td_init,  sizeof(Ctrl_StorageTDType));
	
	Ctrl_ParaTD = ctrl_storage_td_init.para;
	
	SysUsrLog("TD InitPara saved success!\r\n");
}

static void ctr_save_init_dk(void)
{
	sFlash_SectorErase(SFLASH_CTRDK_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_CTRDK_ADDR,  (uint8_t *)&ctrl_storage_dk_init,  sizeof(Ctrl_StorageDKType));
	
	Ctrl_ParaDK = ctrl_storage_dk_init.para;
	
	SysUsrLog("DK InitPara saved success!\r\n");
}


void Ctr_SaveMDPara(void)
{
	memset(&ctrl_storage_md, 0, sizeof(Ctrl_StorageMDType));
	
	sFlash_SectorErase(SFLASH_CTRMD_ADDR);
	
	strncpy(ctrl_storage_md.key, CTR_STORAGE_MD_KEY, 8);
	
	ctrl_storage_md.para = Ctrl_ParaMD;
	
	sFlash_SectorAlignedProgram(SFLASH_CTRMD_ADDR,  (uint8_t *)&ctrl_storage_md,  sizeof(Ctrl_StorageMDType));
	
	SysUsrLog("MD Para saved success!\r\n");
}

void Ctr_SaveJTPara(void)
{
	memset(&ctrl_storage_jt, 0, sizeof(Ctrl_StorageJTType));
	
	sFlash_SectorErase(SFLASH_CTRJT_ADDR);
	
	strncpy(ctrl_storage_jt.key, CTR_STORAGE_JT_KEY, 8);
	
	ctrl_storage_jt.para = Ctrl_ParaJT;
	
	sFlash_SectorAlignedProgram(SFLASH_CTRJT_ADDR,  (uint8_t *)&ctrl_storage_jt,  sizeof(Ctrl_StorageJTType));
	
	SysUsrLog("JT Para saved success!\r\n");
}

void Ctr_SaveDL1Para(void)
{
	memset(&ctrl_storage_dl1, 0, sizeof(Ctrl_StorageDL1Type));
	
	sFlash_SectorErase(SFLASH_CTRDL1_ADDR);
	
	strncpy(ctrl_storage_dl1.key, CTR_STORAGE_DL1_KEY, 8);
	
	ctrl_storage_dl1.para = Ctrl_ParaDL1;
	
	sFlash_SectorAlignedProgram(SFLASH_CTRDL1_ADDR,  (uint8_t *)&ctrl_storage_dl1,  sizeof(Ctrl_StorageDL1Type));
	
	SysUsrLog("DL1 Para saved success!\r\n");
}

void Ctr_SaveDL2Para(void)
{
	memset(&ctrl_storage_dl2, 0, sizeof(Ctrl_StorageDL2Type));
	
	sFlash_SectorErase(SFLASH_CTRDL2_ADDR);
	
	strncpy(ctrl_storage_dl2.key, CTR_STORAGE_DL2_KEY, 8);
	
	ctrl_storage_dl2.para = Ctrl_ParaDL2;
	
	sFlash_SectorAlignedProgram(SFLASH_CTRDL2_ADDR,  (uint8_t *)&ctrl_storage_dl2,  sizeof(Ctrl_StorageDL2Type));
	
	SysUsrLog("DL2 Para saved success!\r\n");
}

void Ctr_SaveSKPara(void)
{
	memset(&ctrl_storage_sk, 0, sizeof(Ctrl_StorageSKType));
	
	sFlash_SectorErase(SFLASH_CTRSK_ADDR);
	
	strncpy(ctrl_storage_sk.key, CTR_STORAGE_SK_KEY, 8);
	
	ctrl_storage_sk.para = Ctrl_ParaSK;
	
	sFlash_SectorAlignedProgram(SFLASH_CTRSK_ADDR,  (uint8_t *)&ctrl_storage_sk,  sizeof(Ctrl_StorageSKType));
	
	SysUsrLog("SK Para saved success!\r\n");
}

void Ctr_SaveTDPara(void)
{
	memset(&ctrl_storage_td, 0, sizeof(Ctrl_StorageTDType));
	
	sFlash_SectorErase(SFLASH_CTRTD_ADDR);
	
	strncpy(ctrl_storage_td.key, CTR_STORAGE_TD_KEY, 8);
	
	ctrl_storage_td.para = Ctrl_ParaTD;
	
	sFlash_SectorAlignedProgram(SFLASH_CTRTD_ADDR,  (uint8_t *)&ctrl_storage_td,  sizeof(Ctrl_StorageTDType));
	
	SysUsrLog("TD Para saved success!\r\n");
}

void Ctr_SaveDKPara(void)
{
	memset(&ctrl_storage_dk, 0, sizeof(Ctrl_StorageDKType));
	
	sFlash_SectorErase(SFLASH_CTRDK_ADDR);
	
	strncpy(ctrl_storage_dk.key, CTR_STORAGE_DK_KEY, 8);
	
	ctrl_storage_dk.para = Ctrl_ParaDK;
	
	sFlash_SectorAlignedProgram(SFLASH_CTRDK_ADDR,  (uint8_t *)&ctrl_storage_dk,  sizeof(Ctrl_StorageDKType));
	
	SysUsrLog("DK Para saved success!\r\n");
}


uint8_t Ctr_LoadMDPara(void)
{
	memset(&ctrl_storage_md, 0, sizeof(Ctrl_StorageMDType));
	
	sFlash_AnywhereRead(SFLASH_CTRMD_ADDR,  (uint8_t *)&ctrl_storage_md,  sizeof(Ctrl_StorageMDType));
	
	if(strcmp(ctrl_storage_md.key, CTR_STORAGE_MD_KEY) != 0)
	{
		ctr_save_init_md();
		return 1;
	}
	
	if(Ctr_CheckMDPara(&ctrl_storage_md.para) != 0)
	{
		ctr_save_init_md();
		return 2;
	}
	
	Ctrl_ParaMD = ctrl_storage_md.para;
	
	SysUsrLog("Load MD para ok!\r\n");
	
	return 0;
}

uint8_t Ctr_LoadJTPara(void)
{
	memset(&ctrl_storage_jt, 0, sizeof(Ctrl_StorageJTType));
	
	sFlash_AnywhereRead(SFLASH_CTRJT_ADDR,  (uint8_t *)&ctrl_storage_jt,  sizeof(Ctrl_StorageJTType));
	
	if(strcmp(ctrl_storage_jt.key, CTR_STORAGE_JT_KEY) != 0)
	{
		ctr_save_init_jt();
		return 1;
	}
	
	if(Ctr_CheckJTPara(&ctrl_storage_jt.para) != 0)
	{
		ctr_save_init_jt();
		return 2;
	}
	
	Ctrl_ParaJT = ctrl_storage_jt.para;
	
	SysUsrLog("Load JT para ok!\r\n");
	
	return 0;
}

uint8_t Ctr_LoadDL1Para(void)
{
	memset(&ctrl_storage_dl1, 0, sizeof(Ctrl_StorageDL1Type));
	
	sFlash_AnywhereRead(SFLASH_CTRDL1_ADDR,  (uint8_t *)&ctrl_storage_dl1,  sizeof(Ctrl_StorageDL1Type));
	
	if(strcmp(ctrl_storage_dl1.key, CTR_STORAGE_DL1_KEY) != 0)
	{
		ctr_save_init_dl1();
		return 1;
	}
	
	if(Ctr_CheckDL1Para(&ctrl_storage_dl1.para) != 0)
	{
		ctr_save_init_dl1();
		return 2;
	}
	
	Ctrl_ParaDL1 = ctrl_storage_dl1.para;
	
	SysUsrLog("Load DL1 para ok!\r\n");
	
	return 0;
}

uint8_t Ctr_LoadDL2Para(void)
{
	memset(&ctrl_storage_dl2, 0, sizeof(Ctrl_StorageDL2Type));
	
	sFlash_AnywhereRead(SFLASH_CTRDL2_ADDR,  (uint8_t *)&ctrl_storage_dl2,  sizeof(Ctrl_StorageDL2Type));
	
	if(strcmp(ctrl_storage_dl2.key, CTR_STORAGE_DL2_KEY) != 0)
	{
		ctr_save_init_dl2();
		return 1;
	}
	
	if(Ctr_CheckDL2Para(&ctrl_storage_dl2.para) != 0)
	{
		ctr_save_init_dl2();
		return 2;
	}
	
	Ctrl_ParaDL2 = ctrl_storage_dl2.para;
	
	SysUsrLog("Load DL2 para ok!\r\n");
	
	return 0;
}

uint8_t Ctr_LoadSKPara(void)
{
	memset(&ctrl_storage_sk, 0, sizeof(Ctrl_StorageSKType));
	
	sFlash_AnywhereRead(SFLASH_CTRSK_ADDR,  (uint8_t *)&ctrl_storage_sk,  sizeof(Ctrl_StorageSKType));
	
	if(strcmp(ctrl_storage_sk.key, CTR_STORAGE_SK_KEY) != 0)
	{
		ctr_save_init_sk();
		return 1;
	}
	
	if(Ctr_CheckSKPara(&ctrl_storage_sk.para) != 0)
	{
		ctr_save_init_sk();
		return 2;
	}
	
	Ctrl_ParaSK = ctrl_storage_sk.para;
	
	SysUsrLog("Load SK para ok!\r\n");
	
	return 0;
}

uint8_t Ctr_LoadTDPara(void)
{
	memset(&ctrl_storage_td, 0, sizeof(Ctrl_StorageTDType));
	
	sFlash_AnywhereRead(SFLASH_CTRTD_ADDR,  (uint8_t *)&ctrl_storage_td,  sizeof(Ctrl_StorageTDType));
	
	if(strcmp(ctrl_storage_td.key, CTR_STORAGE_TD_KEY) != 0)
	{
		ctr_save_init_td();
		return 1;
	}
	
	if(Ctr_CheckTDPara(&ctrl_storage_td.para) != 0)
	{
		ctr_save_init_td();
		return 2;
	}
	
	Ctrl_ParaTD = ctrl_storage_td.para;
	
	SysUsrLog("Load TD para ok!\r\n");
	
	return 0;
}

uint8_t Ctr_LoadDKPara(void)
{
	memset(&ctrl_storage_dk, 0, sizeof(Ctrl_StorageDKType));
	
	sFlash_AnywhereRead(SFLASH_CTRDK_ADDR,  (uint8_t *)&ctrl_storage_dk,  sizeof(Ctrl_StorageDKType));
	
	if(strcmp(ctrl_storage_dk.key, CTR_STORAGE_DK_KEY) != 0)
	{
		ctr_save_init_dk();
		return 1;
	}
	
	if(Ctr_CheckDKPara(&ctrl_storage_dk.para) != 0)
	{
		ctr_save_init_dk();
		return 2;
	}
	
	Ctrl_ParaDK = ctrl_storage_dk.para;
	
	SysUsrLog("Load DK para ok!\r\n");
	
	return 0;
}
